<?php

/**
 * Subject filter form.
 *
 * @package    wiki
 * @subpackage filter
 * @author     Elmaho
 * @version    SVN: $Id: sfDoctrineFormFilterTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class SubjectFormFilter extends BaseSubjectFormFilter
{
  public function configure()
  {
  	$choices = DomainTable::getListArbo();
	$entitePere = new sfWidgetFormSelect(array('choices' => $choices));
	$this->widgetSchema['domain_id'] = $entitePere;
	
	$this->validatorSchema['domain_id'] = new sfValidatorInteger(array('required' => false));
	
  	$this->widgetSchema['title_sjt']->setOption('with_empty', false);
    $this->widgetSchema['summary_sjt']->setOption('with_empty', false);
    $this->widgetSchema['article_sjt']->setOption('with_empty', false);
    
    $this->widgetSchema['created_at'] = new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormJQueryDate(array('config' => '{}')), 'to_date' => new sfWidgetFormJQueryDate(array('config' => '{}')), 'with_empty' => false, 'template' => 'de %from_date% a %to_date%'));
    $this->widgetSchema['updated_at'] = new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormJQueryDate(array('config' => '{}')), 'to_date' => new sfWidgetFormJQueryDate(array('config' => '{}')), 'with_empty' => false, 'template' => 'de %from_date% a %to_date%'));
  }

  public function addDomainIdColumnQuery(Doctrine_Query $query, $field, $values)
  {
  	
  	if(!$values)
  		return;
  		
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }
   
    $ids = $values;
    $domains = array();
    
    $doObject = Doctrine_Core::getTable('Domain')->find($values);
    if($doObject)
    	$domains = $doObject->getNode()->getDescendants();
    
    if($domains)
	    foreach ($domains as $domain) {
	    	$ids[] = $domain->getId();
	    }

    $query
      ->leftJoin($query->getRootAlias().'.Domain dom')
      ->andWhereIn('dom.id', $ids);
    
  }
}

